package com.xnfh.home.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.framework.bean.HomeBannerBean
import com.xnfh.framework.bean.NewVersionHelpMessageBean
import com.xnfh.framework.bean.NewVersionHelpMessageItem
import com.xnfh.home.R
import com.xnfh.home.adapter.MessageDetailAdapter
import com.xnfh.home.databinding.ActivityMessageDetailBinding
import com.xnfh.home.viewmodel.MessageViewModel

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2024/1/4 14:24
 * @UpdateUser: ziheng
 * @UpdateDate: 2024/1/4 14:24
 * @UpdateRemark: 更新说明
 */
class MessageDetailActivity : BaseActivity<ActivityMessageDetailBinding>() {
    override fun getLayoutID(): Int = R.layout.activity_message_detail

    companion object {
        //首页banner传进来是整个bean，不需要请求接口的
        //首页金刚位传进来一个id和一个name作为title
        fun start(context: Context, bundle: Bundle? = null) {
            val intent = Intent(context, MessageDetailActivity::class.java)
            if (bundle != null) {
                intent.putExtra("bundle", bundle)
            }
            context.startActivity(intent)
        }
    }

    //请求消息的id
    private var id: Int? = 0
    private var page = 1
    private lateinit var adapter: MessageDetailAdapter
    private lateinit var viewModel: MessageViewModel
    override fun init() {
        val bundle = intent.getBundleExtra("bundle")
        val dataBean = bundle?.getSerializable("data")
        adapter = MessageDetailAdapter()
        if (dataBean == null) {
            id = bundle?.getInt("id", 0)
            val name = bundle?.getString("name")
            setHeaderTitle(name)
            mBind.messageDetailSmartRefresh.autoRefresh()
        } else {
            val homeBannerBean = dataBean as HomeBannerBean
            setHeaderTitle(homeBannerBean.stTypename)
            mBind.messageDetailSmartRefresh.setEnableRefresh(false)
            mBind.messageDetailSmartRefresh.setEnableLoadMore(false)
            //从首页获取到的数据，copy成当前页面适合的数据
            val adBean = NewVersionHelpMessageItem(
                stTitle = homeBannerBean.stTitle,
                createdat = homeBannerBean.createdat,
                stText = homeBannerBean.stText,
                tableImgList = homeBannerBean.tableImgList
            )
            adapter.setNewInstance(arrayListOf(adBean))
        }
        mBind.messageDetailRecycle.layoutManager = LinearLayoutManager(this)
        mBind.messageDetailRecycle.adapter = adapter
        mBind.messageDetailSmartRefresh.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                viewModel.requestMessageList(page,id!!)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                page++
                viewModel.requestMessageList(page,id!!)
            }
        })
    }

    override fun observe() {
        viewModel = MessageViewModel()
        viewModel.messageList.observe(this,object : BaseStateObserver<NewVersionHelpMessageBean>(){
            override fun getRespDataSuccess(it: NewVersionHelpMessageBean) {
                super.getRespDataSuccess(it)
                mBind.messageDetailSmartRefresh.let {
                    it.finishRefresh()
                    it.finishLoadMore()
                }
                if(it.total == 0){
                    adapter.setNewInstance(null)
                    adapter.setEmptyView(com.xnfh.common.R.layout.adapter_empty_view)
                    return
                }
                if(page == 1){
                    adapter.setNewInstance(it.list.toMutableList())
                }else{
                    adapter.addData(it.list)
                }
            }
        })
    }
}